﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.wMall.Seller
{
    public partial class wfForumInfo : msBsc.Cls.BPDealer
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 得到论坛
        /// </summary>
        private void getForumInfo() {
            string forumId = this.GetParamHtValue("forumId");
            if (string.IsNullOrEmpty(forumId))
            {
                this.ExecJScript(@"window.location.href = ""/ws_ForumList.htm""");
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizSourcePosts sourcePosts = new AppLogic.DataAlc.CBizSourcePosts())
            {
                DataSet ds = sourcePosts.GetPostsForView(forumId);
                if (ds==null||ds.Tables[0].Rows.Count<1)
                {
                    return;
                }
                string retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 100, "");
                this.ExecJScript(@"setForumInfo(" + retJson + ")");
            }
        }

        /// <summary>
        /// 得到论坛回复
        /// </summary>
        private void getReplyList() {
            string forumId = this.GetParamHtValue("forumId");
            if (string.IsNullOrEmpty(forumId))
            {
                this.ExecJScript(@"window.location.href = ""/ws_ForumList.htm""");
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizSourcePosts sourcePosts=new AppLogic.DataAlc.CBizSourcePosts())
            {
                string pIndex = this.GetParamHtValue("pIndex");
                string pRows = this.GetParamHtValue("pRows");
                int pageIndex = 1;
                int pageRows = 11;
                int.TryParse(pIndex, out pageIndex);
                int.TryParse(pRows, out pageRows);
                DataSet ds = sourcePosts.GetRevocerForPageview(pageRows, pageIndex, forumId);
                if (ds == null || ds.Tables[0].Rows.Count < 1)
                {
                    return;
                }
                string retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 100, "");
                this.ExecJScript(@"setReplyList(" + retJson + ",'" + forumId + "');jsPage('divPage', " + Convert.ToInt32(ds.Tables[0].Rows[0]["ROWSCOUNT"].ToString()) + ", " + pageRows + ", " + pageIndex + ", 'goPage');");
            }
        }

        /// <summary>
        /// 回复论坛
        /// </summary>
        private void setReply() {
            string replyInfo = this.GetParamHtValue("replyInfo");
            string replyId = this.GetParamHtValue("replyId");
            string djackId = msBsc.SvRef.UserInfo.Operator;
            string forumId=this.GetParamHtValue("forumId");
            if (string.IsNullOrEmpty(replyInfo))
            {
                this.Alert("回复内容不能为空");
                return;
            }
            if (string.IsNullOrEmpty(replyId))
            {
                this.Alert("回复贴Id不能为空");
                return;
            }
            if (string.IsNullOrEmpty(djackId))
            {
                this.ExecJScript(@"window.location.href = ""/ws_UserLogin.htm""");
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizRecoverPosts recoverPosts=new AppLogic.DataAlc.CBizRecoverPosts())
            {
                string[] ret = recoverPosts.AddRecoverPosts(replyInfo, djackId, replyId, forumId);
                if (ret[0]=="OK")
                {
                    this.ExecJScript(@"$("".reply"").hide();
                        $("".replyTxt"").val("""");replyId="""";");
                    getReplyList();
                }
                else
                {
                    this.Alert(ret[1]);
                }
            }
        }

        public override void DoAction()
        {
            if (this.strAction=="init")
            {
                getForumInfo();
                getReplyList();
            }
            else if (this.strAction=="reply")
            {
                setReply();
            }
            else if (this.strAction == "getReply")
            {
                getReplyList();
            }
        }
    }
}